﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;

namespace VSIXUtilities.EventsListeners
{
	public class VsShellPropertyEvents : IVsShellPropertyEvents, IDisposable
	{
		private IVsShell vsShell = null;
		private uint pdwCookie;
		public delegate void ShellPropertyChangedHandler(int propid, object var);
		public event ShellPropertyChangedHandler ShellPropertyChanged;


		public VsShellPropertyEvents()
		{
			vsShell = ServiceProvider.GlobalProvider.GetService(typeof(SVsShell)) as IVsShell;
			if(vsShell != null)
			{
				vsShell.AdviseShellPropertyChanges(this, out pdwCookie);
			}
		}
		#region IVsShellPropertyEvents Members

		public int OnShellPropertyChange(int propid, object var)
		{
			if(ShellPropertyChanged != null)
			{
				ShellPropertyChanged(propid, var);
			}
			return VSConstants.S_OK;
		}

		#endregion

		#region IDisposable Members

		public void Dispose()
		{
			if(vsShell != null && pdwCookie != 0)
			{
				vsShell.UnadviseShellPropertyChanges(pdwCookie);
			}
		}

		#endregion
	}
}
